<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>聚合支付 - MarsPay支付系统</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #ffe60f;
            --primary-dark: #ffd700;
            --primary-light: #fff8c4;
            --text-dark: #333;
            --text-gray: #666;
        }
        
        body {
            background: linear-gradient(180deg, #fff 0%, #f8f8f8 100%);
            min-height: 100vh;
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }
        
        .payment-header {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-dark) 100%);
            color: var(--text-dark);
            padding: 20px 0;
            box-shadow: 0 2px 10px rgba(255, 230, 15, 0.3);
        }
        
        .payment-header h1 {
            font-size: 1.5rem;
            font-weight: 600;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }
        
        .union-icons {
            display: flex;
            gap: 5px;
        }
        
        .icon-badge {
            width: 36px;
            height: 36px;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.3rem;
        }
        
        .wechat-badge {
            background: #07c160;
        }
        
        .alipay-badge {
            background: #1678ff;
        }
        
        .payment-container {
            max-width: 1200px;
            margin: 30px auto;
            padding: 0 15px;
        }
        
        .payment-main {
            background: white;
            border-radius: 20px;
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
            overflow: hidden;
        }
        
        .payment-content {
            display: flex;
            min-height: 600px;
        }
        
        /* 左侧订单信息 */
        .order-section {
            flex: 1;
            padding: 40px;
            background: linear-gradient(135deg, var(--primary-light) 0%, #fff 100%);
            border-right: 3px solid var(--primary-color);
        }
        
        .section-title {
            font-size: 1.3rem;
            font-weight: 600;
            color: var(--text-dark);
            margin-bottom: 30px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .order-item {
            padding: 20px 0;
            border-bottom: 1px dashed #e8e8e8;
        }
        
        .order-item:last-child {
            border-bottom: none;
        }
        
        .order-label {
            font-size: 0.95rem;
            color: var(--text-gray);
            margin-bottom: 8px;
        }
        
        .order-value {
            font-size: 1.1rem;
            font-weight: 600;
            color: var(--text-dark);
        }
        
        .order-no {
            font-family: 'Courier New', monospace;
            background: white;
            padding: 8px 12px;
            border-radius: 6px;
            font-size: 0.95rem;
        }
        
        .amount-display {
            font-size: 2.5rem;
            font-weight: bold;
            color: #ff6b6b;
        }
        
        .support-methods {
            margin-top: 30px;
            padding: 20px;
            background: white;
            border-radius: 12px;
        }
        
        .support-title {
            font-size: 1rem;
            color: var(--text-gray);
            margin-bottom: 15px;
        }
        
        .method-icons {
            display: flex;
            gap: 15px;
        }
        
        .method-item {
            flex: 1;
            padding: 12px;
            background: linear-gradient(135deg, #f8f8f8 0%, #fff 100%);
            border-radius: 8px;
            text-align: center;
            border: 2px solid transparent;
        }
        
        .method-item.wechat {
            border-color: #07c160;
        }
        
        .method-item.alipay {
            border-color: #1678ff;
        }
        
        .method-icon {
            font-size: 2rem;
            margin-bottom: 5px;
        }
        
        .method-name {
            font-size: 0.85rem;
            color: var(--text-dark);
            font-weight: 500;
        }
        
        /* 右侧二维码 */
        .qr-section {
            flex: 1;
            padding: 40px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }
        
        .qr-title {
            font-size: 1.3rem;
            font-weight: 600;
            color: var(--text-dark);
            margin-bottom: 10px;
            text-align: center;
        }
        
        .qr-subtitle {
            color: var(--text-gray);
            margin-bottom: 30px;
            text-align: center;
        }
        
        .qr-wrapper {
            position: relative;
            padding: 20px;
            background: linear-gradient(135deg, #f8f8f8 0%, white 100%);
            border-radius: 20px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            margin-bottom: 25px;
        }
        
        .qr-code-img {
            width: 280px;
            height: 280px;
            border: 4px solid var(--primary-color);
            border-radius: 12px;
            padding: 8px;
            background: white;
        }
        
        .support-tips {
            background: linear-gradient(135deg, #e6f7ff 0%, #fff 100%);
            border: 2px solid #1678ff;
            border-radius: 12px;
            padding: 15px 20px;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .tips-icons {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-bottom: 10px;
        }
        
        .tips-text {
            color: var(--text-dark);
            font-size: 0.95rem;
        }
        
        .countdown-box {
            background: linear-gradient(135deg, #fff5e6 0%, white 100%);
            border: 2px solid var(--primary-color);
            border-radius: 50px;
            padding: 12px 24px;
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 30px;
        }
        
        .countdown-time {
            font-size: 1.3rem;
            font-weight: bold;
            color: #ff6b6b;
            font-family: 'Courier New', monospace;
        }
        
        .action-buttons {
            width: 100%;
            max-width: 350px;
            display: grid;
            gap: 12px;
        }
        
        .btn-refresh {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-dark) 100%);
            color: var(--text-dark);
            border: none;
            padding: 14px 30px;
            font-size: 1.05rem;
            font-weight: 600;
            border-radius: 50px;
            box-shadow: 0 4px 15px rgba(255, 230, 15, 0.4);
        }
        
        .btn-refresh:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(255, 230, 15, 0.5);
        }
        
        .btn-back {
            background: white;
            color: var(--text-dark);
            border: 2px solid #e8e8e8;
            padding: 14px 30px;
            font-size: 1.05rem;
            font-weight: 500;
            border-radius: 50px;
        }
        
        .btn-back:hover {
            border-color: var(--primary-color);
            background: var(--primary-light);
        }
        
        @media (max-width: 992px) {
            .payment-content {
                flex-direction: column;
            }
            
            .order-section {
                border-right: none;
                border-bottom: 3px solid var(--primary-color);
            }
        }
    </style>
</head>
<body>
    <!-- 头部 -->
    <div class="payment-header">
        <div class="container">
            <h1>
                <div class="union-icons">
                    <div class="icon-badge wechat-badge">
                        <i class="bi bi-wechat"></i>
                    </div>
                    <div class="icon-badge alipay-badge">
                        <i class="bi bi-alipay"></i>
                    </div>
                </div>
                聚合支付
            </h1>
        </div>
    </div>

    <!-- 主体内容 -->
    <div class="payment-container">
        <div class="payment-main">
            <div class="payment-content">
                <!-- 左侧：订单信息 -->
                <div class="order-section">
                    <div class="section-title">
                        <i class="bi bi-receipt"></i>
                        订单信息
                    </div>
                    
                    <div class="order-item">
                        <div class="order-label">订单号</div>
                        <div class="order-value">
                            <span class="order-no" th:text="${order.orderNo}">ORDER001</span>
                        </div>
                    </div>
                    
                    <div class="order-item">
                        <div class="order-label">商品名称</div>
                        <div class="order-value" th:text="${order.productName}">商品名称</div>
                    </div>
                    
                    <div class="order-item">
                        <div class="order-label">支付金额</div>
                        <div class="order-value">
                            <span class="amount-display" th:text="'¥' + ${order.amount}">¥0.00</span>
                        </div>
                    </div>
                    
                    <!-- 支持的支付方式 -->
                    <div class="support-methods">
                        <div class="support-title">📱 支持以下支付方式</div>
                        <div class="method-icons">
                            <div class="method-item wechat">
                                <div class="method-icon" style="color: #07c160;">
                                    <i class="bi bi-wechat"></i>
                                </div>
                                <div class="method-name">微信支付</div>
                            </div>
                            <div class="method-item alipay">
                                <div class="method-icon" style="color: #1678ff;">
                                    <i class="bi bi-alipay"></i>
                                </div>
                                <div class="method-name">支付宝</div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 右侧：二维码 -->
                <div class="qr-section">
                    <h3 class="qr-title">扫码即可支付</h3>
                    <p class="qr-subtitle">微信/支付宝扫一扫，自动识别支付方式</p>
                    
                    <div class="qr-wrapper">
                        <img th:src="${qrCode}" alt="聚合支付二维码" class="qr-code-img">
                    </div>

                    <div class="support-tips">
                        <div class="tips-icons">
                            <i class="bi bi-wechat" style="font-size: 1.5rem; color: #07c160;"></i>
                            <span style="font-size: 1.2rem;">+</span>
                            <i class="bi bi-alipay" style="font-size: 1.5rem; color: #1678ff;"></i>
                        </div>
                        <div class="tips-text">一码通付，智能识别支付方式</div>
                    </div>

                    <div class="countdown-box">
                        <i class="bi bi-clock-history" style="font-size: 1.3rem; color: #ff6b6b;"></i>
                        <span style="color: #333; font-size: 0.95rem;">请在</span>
                        <span class="countdown-time" id="countdown">5:00</span>
                        <span style="color: #333; font-size: 0.95rem;">内完成支付</span>
                    </div>

                    <div class="action-buttons">
                        <button class="btn btn-refresh" onclick="checkPaymentStatus()">
                            <i class="bi bi-arrow-clockwise"></i> 刷新支付状态
                        </button>
                        <a href="/order/list" class="btn btn-back text-decoration-none text-center">
                            <i class="bi bi-list-ul"></i> 返回订单列表
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script th:inline="javascript">
        const orderNo = /*[[${order.orderNo}]]*/ '';
        let checkInterval = null;
        let countdownInterval = null;
        let timeLeft = 300;

        function startPolling() {
            checkInterval = setInterval(function() {
                checkPaymentStatus(false);
            }, 3000);
        }

        function startCountdown() {
            countdownInterval = setInterval(function() {
                timeLeft--;
                const minutes = Math.floor(timeLeft / 60);
                const seconds = timeLeft % 60;
                document.getElementById('countdown').innerText = 
                    `${minutes}:${seconds.toString().padStart(2, '0')}`;
                
                if (timeLeft <= 0) {
                    clearInterval(countdownInterval);
                    clearInterval(checkInterval);
                    alert('⏰ 订单已超时，请重新下单');
                }
            }, 1000);
        }

        function checkPaymentStatus(showLoading = true) {
            fetch(`/pay/query/${orderNo}`)
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        const order = data.data.order;
                        const status = order.status;
                        
                        if (status === 2) {
                            clearInterval(checkInterval);
                            clearInterval(countdownInterval);
                            alert('✅ 支付成功！');
                            window.location.href = '/pay/success';
                        } else if (status === 3) {
                            clearInterval(checkInterval);
                            alert('❌ 支付失败，请重新支付');
                        } else if (showLoading) {
                            alert('⏳ 等待支付中...');
                        }
                    } else if (showLoading) {
                        alert('❌ 查询失败：' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    if (showLoading) {
                        alert('❌ 查询失败，请重试');
                    }
                });
        }

        window.onload = function() {
            startPolling();
            startCountdown();
        };

        window.onbeforeunload = function() {
            if (checkInterval) clearInterval(checkInterval);
            if (countdownInterval) clearInterval(countdownInterval);
        };
    </script>
</body>
</html>

